package base;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import main.RoomLibrary;

public abstract class Window {

	private static final long serialVersionUID = 1L;
	public static final int width = 170;
	public static final int height = 310;

	private boolean ignoreRight = false;
	private boolean ignoreLeft = false;
	private boolean ignoreUp = false;
	private boolean ignoreDown = false;
	private String name = "";
	protected Rectangle clip = new Rectangle( 0 , 0 , width , height );

	public Window( String name , String ... rooms )
	{
		this.name = name;
		for( String room : rooms )
		{
			int idBs = room.indexOf( '[' );
			if( idBs == -1 )
			{
				RoomLibrary.addDevice( room , 0 , this );
				continue;
			}
			String tempName = room.substring( 0 , idBs );
			String strID = room.substring( idBs+1 , room.indexOf( "]" ) );
			int id = 0;
			try{ id = Integer.parseInt( strID ); } catch( Exception e ){ }
			RoomLibrary.addDevice( tempName , id , this );
		}
	}

	public String getName( )
	{
		return name;
	}

	public int getX( ){return clip.x;}
	public int getY( ){return clip.y;}
	public int getWidth( ){return clip.width;}
	public int getHeight( ){return clip.height;}
	public Rectangle getClip( ){return clip;}

	public void setIgnoreRight( boolean ignore )
	{
		this.ignoreRight = ignore;
	}

	public void setIgnoreLeft( boolean ignore )
	{
		this.ignoreLeft = ignore;
	}

	public void setIgnoreUp( boolean ignore )
	{
		this.ignoreUp = ignore;
	}

	public void setIgnoreDown( boolean ignore )
	{
		this.ignoreDown = ignore;
	}

	public void update( ){ }

	//TODO Event Logging
	private Point start = null;
	private boolean focus = false;
	public boolean mouseClicked( MouseEvent arg0 )
	{
		if( !clip.contains( arg0.getX( ) , arg0.getY( ) ) )
			return false;
		focus = true;
		return true;
	}
	public boolean mousePressed( MouseEvent arg0 )
	{
		if( !clip.contains( arg0.getX( ) , arg0.getY( ) ) )
			return false;
		start = new Point( arg0.getX( ) , arg0.getY( ) );
		fingerPressed( new Point( start ) );
		focus = true;
		return true;
	}
	public boolean mouseReleased( MouseEvent arg0 )
	{
		if( !focus )
			return false;
		focus = false;
		fingerReleased( new Point( arg0.getX( ) , arg0.getY( ) ) );
		return true;
	}

	public boolean mouseEntered( MouseEvent arg0 )
	{
		if( !clip.contains( arg0.getX( ) , arg0.getY( ) ) )
			return false;
		return true;
	}
	public boolean mouseExited( MouseEvent arg0 )
	{
		if( !clip.contains( arg0.getX( ) , arg0.getY( ) ) )
			return false;
		return true;
	}
	public boolean mouseDragged( MouseEvent arg0 )
	{
		if( !clip.contains( arg0.getX( ) , arg0.getY( ) ) && !focus )
			return false;
		if( start == null )
		{
			start = new Point( arg0.getX( ) , arg0.getY( ) );
			fingerPressed( new Point( start ) );
			focus = true;
		}
		Point end = new Point( arg0.getX( ) , arg0.getY( ) );

		int deltaX = end.x - start.x;
		int deltaY = end.y - start.y;

		if( deltaX < 0 && ignoreLeft )
			deltaX = 0;
		if( deltaX > 0 && ignoreRight )
			deltaX = 0;
		if( deltaY < 0 && ignoreUp )
			deltaY = 0;
		if( deltaY > 0 && ignoreDown )
			deltaY = 0;
		if( deltaX == 0 && deltaY == 0 )
			return true;

		gestureHorizontal( deltaX );
		gestureVertical( deltaY );
		return true;
	}
	public boolean mouseMoved(MouseEvent arg0)
	{
		if( !clip.contains( arg0.getX( ) , arg0.getY( ) ) )
			return false;
		return true;
	}

	//TODO ABSTRACT
	public abstract void fingerPressed( Point location );
	public abstract void fingerReleased( Point location );
	public abstract void gestureVertical( int amount );
	public abstract void gestureHorizontal( int amount );
	public abstract void draw( Graphics2D g2d );
}
